#include "pageswitch.h"
#include "ui_pageswitch.h"

PageSwitch::PageSwitch(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::PageSwitch)
{
    ui->setupUi(this);
    setAutoFillBackground(true);  //设置自动填充
    QPalette palette = BKGCLR;
    setPalette(palette);

    switch_page_car_.append(ui->widget);
    //--TC
    port_name_list_[0][0]<<"VCU-2"<<"VCU-1"<<"EVR"<<"RIOM10"<<"RIOM10"<<"DDU"
                         <<"LCU"<<"ATC"<<"PIS"<<"HVAC"<<"BCE"<<"FCU"
                         <<"EDCU8"<<"ACE"<<"BMS"<<"spare"<<"spare"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare"<<"spare"<<"spare";
    port_name_list_[0][1]<<"VCU-2"<<"VCU-1"<<"EVR"<<"RIOM10"<<"RIOM10"<<"DDU"
                         <<"LCU"<<"ATC"<<"PIS"<<"HVAC"<<"BCE"<<"FCU"
                         <<"EDCU7"<<"ACE"<<"BMS"<<"BCE"<<"SG"<<"SG"
                         <<"spare"<<"spare"<<"spare"<<"spare"<<"ACE"<<"spare";
    //--Mp
    port_name_list_[1][0]<<"LCU"<<"ICDS"<<"EDCU8"<<"HVAC"<<"TCU"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare"<<"spare"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare";
    port_name_list_[1][1]<<"LCU"<<"ICDS"<<"EDCU7"<<"HVAC"<<"TCU"<<"TCU"
                         <<"ECR"<<"ICDS"<<"spare"<<"spare"<<"spare"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare";
    //--M1
    port_name_list_[2][0]<<"LCU"<<"EDCU8"<<"HVAC"<<"BCE"<<"ACE"<<"spare"
                         <<"TCU"<<"spare"<<"spare"<<"spare"<<"spare"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare";
    port_name_list_[2][1]<<"LCU"<<"EDCU7"<<"HVAC"<<"BCE"<<"TCU"<<"spare"
                         <<"TCU"<<"ACE"<<"ACE"<<"BCE"<<"spare"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare";
    //--M2
    port_name_list_[3][0]<<"LCU"<<"EDCU8"<<"HVAC"<<"BCE"<<"ACE"<<"EVR"
                         <<"TCU"<<"spare"<<"spare"<<"spare"<<"spare"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare";
    port_name_list_[3][1]<<"LCU"<<"EDCU7"<<"HVAC"<<"BCE"<<"TCU"<<"EVR"
                         <<"TCU"<<"ACE"<<"ACE"<<"BCE"<<"EVR"<<"spare"
                         <<"spare"<<"spare"<<"spare"<<"spare";
    //--Tc for train 7th
    port_name_list_[4][0]<<"VCU-2"<<"VCU-1"<<"EVR"<<"RIOM10"<<"RIOM10"<<"DDU"
                         <<"LCU"<<"ATC"<<"PIS"<<"HVAC"<<"BCE"<<"FCU"
                         <<"EDCU8"<<"ACE"<<"BMS"<<"spare"<<"spare"<<"spare"
                         <<"spare"<<"TCDS"<<"spare"<<"spare"<<"spare"<<"spare";
    port_name_list_[4][1]<<"VCU-2"<<"VCU-1"<<"EVR"<<"RIOM10"<<"RIOM10"<<"DDU"
                         <<"LCU"<<"ATC"<<"PIS"<<"HVAC"<<"BCE"<<"FCU"
                         <<"EDCU7"<<"ACE"<<"BMS"<<"BCE"<<"SG"<<"SG"
                         <<"spare"<<"TCDS"<<"spare"<<"spare"<<"ACE"<<"spare";
    InitSwithPages();
    CreateBtnSlots();
    ui->CS2_Label->hide();
    ui->CS1_Label->hide();
}

void PageSwitch::InitSwithPages()
{
    for(int i=0;i<6;i++)
    {
        QWidget *w = new QWidget(this);
        w->setGeometry(100,120,1024,441);
        switch_page_car_.append(w);
        w->hide();
        for(int j=0;j<2;j++)
        {
            if(i==0||i==5)
            {
                Aquam8628 *switch_8628 = new Aquam8628(w);
                switch_8628->setGeometry(534-494*j,56,450,329);
                switch_8628->InitSwtichName("Aquam8628");
                switch_8628->InitSwitchPorts(port_name_list_[0][j]);
                switch_list_[i].append(switch_8628);
            }
            else if(i==1||i==4)
            {
                Aquam8212 *switch_8120 = new Aquam8212(w);
                switch_8120->setGeometry(534-494*j,56,450,329);
                switch_8120->InitSwtichName("Aquam8120");
                switch_8120->InitSwitchPorts(port_name_list_[1][j]);
                switch_list_[i].append(switch_8120);
            }
            else if(i==2)
            {
                Aquam8212 *switch_8120 = new Aquam8212(w);
                switch_8120->setGeometry(534-494*j,56,450,329);
                switch_8120->InitSwtichName("Aquam8120");
                switch_8120->InitSwitchPorts(port_name_list_[2][j]);
                switch_list_[i].append(switch_8120);
            }
            else if(i==3)
            {
                Aquam8212 *switch_8120 = new Aquam8212(w);
                switch_8120->setGeometry(534-494*j,56,450,329);
                switch_8120->InitSwtichName("Aquam8120");
                switch_8120->InitSwitchPorts(port_name_list_[3][j]);
                switch_list_[i].append(switch_8120);
            }
        }
    }
    main_switch_ports_[0].append(ui->switch1_x25_1);main_switch_ports_[0].append(ui->switch1_x26_1);
    main_switch_ports_[0].append(ui->switch2_x25_1);main_switch_ports_[0].append(ui->switch2_x26_1);

    main_switch_ports_[1].append(ui->switch1_x17_2);main_switch_ports_[1].append(ui->switch1_x18_2);
    main_switch_ports_[1].append(ui->switch2_x17_2);main_switch_ports_[1].append(ui->switch2_x18_2);

    main_switch_ports_[2].append(ui->switch1_x17_3);main_switch_ports_[2].append(ui->switch1_x18_3);
    main_switch_ports_[2].append(ui->switch2_x17_3);main_switch_ports_[2].append(ui->switch2_x18_3);

    main_switch_ports_[3].append(ui->switch1_x17_4);main_switch_ports_[3].append(ui->switch1_x18_4);
    main_switch_ports_[3].append(ui->switch2_x17_4);main_switch_ports_[3].append(ui->switch2_x18_4);

    main_switch_ports_[4].append(ui->switch1_x17_5);main_switch_ports_[4].append(ui->switch1_x18_5);
    main_switch_ports_[4].append(ui->switch2_x17_5);main_switch_ports_[4].append(ui->switch2_x18_5);

    main_switch_ports_[5].append(ui->switch1_x25_6);main_switch_ports_[5].append(ui->switch1_x26_6);
    main_switch_ports_[5].append(ui->switch2_x25_6);main_switch_ports_[5].append(ui->switch2_x26_6);
}

void PageSwitch::CreateBtnSlots()
{
    change_sal_btn_list_.append(ui->Btn_Train);
    change_sal_btn_list_.append(ui->Btn_Tc1);
    change_sal_btn_list_.append(ui->Btn_Mp1);
    change_sal_btn_list_.append(ui->Btn_M1);
    change_sal_btn_list_.append(ui->Btn_M2);
    change_sal_btn_list_.append(ui->Btn_Mp2);
    change_sal_btn_list_.append(ui->Btn_Tc2);
    QSignalMapper *_pSalSelMapper = new QSignalMapper(this);
    for (int i = 0; i < change_sal_btn_list_.size(); ++i) {
        connect(change_sal_btn_list_[i], SIGNAL(clicked()), _pSalSelMapper, SLOT(map()));
        _pSalSelMapper->setMapping(change_sal_btn_list_[i], i);
    }
    connect(_pSalSelMapper, SIGNAL(mapped(int)),this, SLOT(slot_SalSelect(int)));
}

void PageSwitch::slot_SalSelect(int sal)
{
    for (int i=0;i<switch_page_car_.length() ; ++i) {
        switch_page_car_[i]->setVisible(i==sal);
    }
    changeComment(sal);
}

void PageSwitch::changeComment(int sal)
{
    if(sal == 0)
    {
        ui->CS2_Label->hide();
        ui->CS1_Label->hide();
        ui->CommentFullDuplex->setText("正常(全双工)");
        ui->CommentHalfDuplex->setText("正常(半双工)");
    }
    else
    {
        ui->CS2_Label->show();
        ui->CS1_Label->show();
        ui->CommentFullDuplex->setText("正常(百兆全双工)");
        ui->CommentHalfDuplex->setText("正常(百兆半双工)");
    }
}

PageSwitch::~PageSwitch()
{
    delete ui;
}

void PageSwitch::on_Btn_Return_clicked()
{
    emit returnToMaintMenu();
}

void PageSwitch::setSwitchPortStat(int car, int switch_index, uint32_t IO_stat, uint32_t duplex_flt_stat)
{
    if(car < 0 || car > 5)
        return;
    int swith_port_count = switch_list_[car][switch_index]->portCount();
    for (int i=0;i<swith_port_count ;i++ ) {
        int single_port_io_stat = (IO_stat >> i) & 0x00000001;
        int single_port_duplex_stat = (duplex_flt_stat >> i) & 0x00000001;
        switch_list_[car][switch_index]->updatePortStat(i,single_port_io_stat,single_port_duplex_stat);
    }
    //主干网端口
    int main_switch_port_start_index = (car==0||car==5) ? 24:16;
    int main_switch_port1_stat = (IO_stat >> main_switch_port_start_index) & 0x00000001;
    int main_switch_port1_duplex_stat = (duplex_flt_stat >> main_switch_port_start_index) & 0x00000001;
    updateMainSwitchPortStat(car,switch_index*2,main_switch_port1_stat,main_switch_port1_duplex_stat);
    int main_switch_port2_stat = (IO_stat >>(main_switch_port_start_index+1) ) & 0x00000001;
    int main_switch_port2_duplex_stat = (duplex_flt_stat >> (main_switch_port_start_index+1)) & 0x00000001;
    updateMainSwitchPortStat(car,switch_index*2+1,main_switch_port2_stat,main_switch_port2_duplex_stat);
}

void PageSwitch::updateMainSwitchPortStat(int car,int index, int port_stat, int duplex_stat)
{
    if(car < 0 || car > 5)
        return;
    if(port_stat == 0)
        main_switch_ports_[car][index]->setStyleSheet("background-color: red;border-radius:8px;");
    else if(port_stat == 1)
    {
        if(duplex_stat == 1)
            main_switch_ports_[car][index]->setStyleSheet("background-color:rgb(219,219,0);border-radius:8px;");
        else if(duplex_stat == 0)
            main_switch_ports_[car][index]->setStyleSheet("background-color: rgb(0, 255, 0);border-radius:8px;");
    }
}

void PageSwitch::updateSwitchPortName(int trainNumber)
{
    if(trainNumber == 7)
    {
        switch_list_[0][0]->updatePortsName(port_name_list_[4][0]);
        switch_list_[0][1]->updatePortsName(port_name_list_[4][1]);

        switch_list_[5][0]->updatePortsName(port_name_list_[4][0]);
        switch_list_[5][1]->updatePortsName(port_name_list_[4][1]);
    }
    else
    {
        switch_list_[0][0]->updatePortsName(port_name_list_[0][0]);
        switch_list_[0][1]->updatePortsName(port_name_list_[0][1]);

        switch_list_[5][0]->updatePortsName(port_name_list_[0][0]);
        switch_list_[5][1]->updatePortsName(port_name_list_[0][1]);
    }
}
